<template>
  <div class="login-container">
    <!-- 导航栏 -->
    <van-nav-bar
      class="page-nav-bar"
      @click-left="$router.back()"
      left-arrow
      title="注册"
    />
    <!-- /导航栏 -->

    <van-form @submit="onSubmit" :show-error="false">
      <van-field
        v-model="user.nickname"
        name="nickname"
        label="昵称"
        placeholder="请输入昵称"
        type="String"
        :rules="rules.nickname"
        clearable
      >
      </van-field>

      <van-field
        v-model="user.mobile"
        name="mobile"
        label="手机号"
        placeholder="请输入手机号"
        type="number"
        :rules="rules.mobile"
        clearable
      >
      </van-field>
      <van-field
        v-model="user.password"
        name="password"
        type="password"
        label="密码"
        placeholder="请输入密码"
        :rules="rules.password"
        clearable
      >
      </van-field>

      <van-field
        v-model="user.asyncValidator"
        label="确认密码"
        placeholder="请再次输入密码"
        name="asyncValidator"
        type="password"
        clearable
      >
      </van-field>
      <van-field name="uploader" label="请选择头像">
        <template #input>
          <van-uploader v-model="uploader" />
        </template>
      </van-field>
      <div class="login-btn-wrap">
        <van-button
          class="login-btn"
          round
          block
          type="info"
          native-type="submit"
          @click="signUser"
          >立即注册</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { signUser } from "@/api/cuixingkui/user";
export default {
  name: "Sige",
  data() {
    return {
      isShow: false,
      uploader: [],
      user: {
        mobile: "",
        password: "",
        nickname: "",
        // avatar: "",
        asyncValidator: "",
      },

      rules: {
        mobile: [
          { required: true, trigger: blur, message: "请输入手机号" },
          {
            pattern: /^1[3|5|7|8]\d{9}$/,
            message: "手机号格式错误",
          },
        ],
        password: [
          { required: true, trigger: blur, message: "请输入新密码" },
          {
            pattern: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/,
            message: "密码至少包含 数字和英文，长度6-20",
          },
        ],
        nickname: [{ required: true, trigger: blur, message: "请输入昵称" }],
      },
    };
  },
  methods: {
    onSubmit(val) {
      console.log(val);
    },

    //注册
    async signUser() {
      try {
        await signUser(this.user);
        // console.log(res);
        this.$toast.success("注册成功！");
        await this.$router.push("/login");
      } catch (error) {
        console.log(error);
        return this.$toast.success("注册失败！");
      }
    },
  },
};
</script>

<style lang="less" scoped>
.login-btn {
  width: 650px;
  margin-left: 50px;
}
</style>
